package com.itheima.utils;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;

import java.io.ByteArrayInputStream;
/**
 * @author chenxys
 * @Description: 阿里云文件上传工具类
 * @date 2022/5/22 11:23
 */
public class AliUtils {
    /**
     * ENDPOINT
     * Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
     */
    private static final String ENDPOINT = "https://oss-cn-hangzhou.aliyuncs.com";
    /**
     * ACCESS_KEY_ID
     * 阿里云账号AccessKey拥有所有API的访问权限，风险很高。
     * 强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
     */
    private static final String ACCESS_KEY_ID = "LTAI5tMrSkCaNYSisq7gAB42";
    /**
     * ACCESS_KEY_SECRET
     */
    private static final String ACCESS_KEY_SECRET = "QeQvFoWnSABy2hZeBww7zzsOpj1Wsr";

    /**
     * BUCKET_NAME
     * 填写Bucket名称，例如examplebucket。
     */
    private static final String BUCKET_NAME = "dark-health";

    /**
     * 阿里云文件上传
     * @author chenxys
     * @param filePath
     * @return void
     */
    public static void uploadAli(String filePath){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        try {
            String content = "Hello OSS";
            ossClient.putObject(BUCKET_NAME, filePath, new ByteArrayInputStream(content.getBytes()));
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 删除文件
     * @author chenxys
     * @param filePath
     * @return void
     */
    public static void deleteALi(String filePath){
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        try {
            // 删除文件。
            ossClient.deleteObject(BUCKET_NAME, filePath);
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}
